#include <queue>
#include <utility>
#include <vector>
using namespace std;

class Solution
{
    //不是0，应该是大O
    int m{}, n{};
    int dx[4] = {-1, 1, 0, 0};
    int dy[4] = {0, 0, -1, 1};
public:
    void solve(vector<vector<char>>& board)
    {
        m = board.size(), n = board[0].size();
        for(int i=0; i<m; i++)
        {
            if(board[i][0]=='O')
                bfs(board, i, 0);
            if(board[i][n-1]=='O')
                bfs(board, i, n-1);
        }

        for(int j=1; j<n-1; j++)
        {
            if(board[0][j]=='O')
                bfs(board, 0, j);
            if(board[m-1][j]=='O')
                bfs(board, m-1, j);
        }
        //所有边界上的0以及他们的连通块已经变成'.'
        for(int i=0; i<m; i++)
        {
            for(int j=0; j<n; j++)
            {
                if(board[i][j]=='O')
                    board[i][j] = 'X';
                else if(board[i][j] == '.')
                    board[i][j] = 'O';
            }
        }
    }

    void bfs(vector<vector<char>>& board, int i, int j)
    {
        queue<pair<int, int>> q;
        q.emplace(i, j);
        board[i][j] = '.';

        while(!q.empty())
        {
            auto [a, b] = q.front();
            q.pop();
            for(int k=0; k<4; k++)
            {
                int x = a+dx[k], y = b+dy[k];
                if(x>=0 && x<m && y>=0 && y<n && board[x][y]=='O')
                {
                    q.emplace(x, y);
                    board[x][y] = '.';
                }
            }
        }
    }
};
